package controlador.ventas;

import controlador.ServletHelper;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import modelo.Boleta;
import modelo.Caja;
import modelo.Usuarios;
import persistencia.BoletaDetalleComposite;
import persistencia.VentasBean;

/**
 *
 * @author Felipe
 */
@WebServlet(name = "FinalizarVenta", urlPatterns = {"/FinalizarVenta"})
public class FinalizarVenta extends HttpServlet {

    @Resource(name = "jdbc/HOLAJDBC")
    private DataSource ds;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Connection conexion = null;
        boolean condi = ServletHelper.validarUsu(request, response);
        if (condi) {
            try {

                int descuentos = 0;
                
                conexion = ds.getConnection();
                conexion.setAutoCommit(false);
                HttpSession session = request.getSession();
                Usuarios usu = (Usuarios) session.getAttribute("USUARIO");
                BoletaDetalleComposite bdc = (BoletaDetalleComposite) session.getAttribute("BOLETA_DETALLE");
                if (bdc != null) {
                    VentasBean ventaBean = new VentasBean(conexion);
                    Boleta bol = new Boleta();
                    bdc.setBoleta(bol);
                    Caja caja = (Caja) session.getAttribute("CAJA");
                    String strdesc = request.getParameter("DESCUENTO");
                    if (strdesc != null){      
                        descuentos = Integer.parseInt(strdesc);
                    }
                    caja = ventaBean.realizarVenta(bdc, usu.getRutUsuario(), descuentos, caja);
                    session.setAttribute("CAJA", caja);
                    session.removeAttribute("BOLETA_DETALLE");
                    conexion.commit();
                    session.setAttribute("Mensaje","Venta Realizada");
                    response.sendRedirect("/TT2/index.jsp");
                } else {
                    session.setAttribute("Mensaje", "Debe abrir la Venta");
                    response.sendRedirect("/TT2/index.jsp");
                    //  request.getRequestDispatcher("/index.jsp").forward(request, response);

                }
            } catch (Exception ex) {
                try {
                    Logger.getLogger(FinalizarVenta.class.getName()).log(Level.SEVERE, null, ex);
                    conexion.rollback();
                } catch (SQLException ex1) {
                    Logger.getLogger(FinalizarVenta.class.getName()).log(Level.SEVERE, null, ex1);
                }
              
            } finally {
                try {
                    conexion.close();
                } catch (SQLException noGestionar) {
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
